package com.nets.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.*;

/**
 * author sig
 * since 2017/2/11
 */
public class ZipUtil {

    public static byte[] zip(byte[] data) {
        byte[] bytes = null;
        ByteArrayOutputStream bos = null;
        ZipOutputStream zos = null;

        try {
            bos = new ByteArrayOutputStream();
            zos = new ZipOutputStream(bos);
            ZipEntry entry = new ZipEntry("zip");
            entry.setSize(data.length);
            zos.putNextEntry(entry);
            zos.write(data);
            zos.closeEntry();
            bytes = bos.toByteArray();
        } catch (Exception e) {
            LogUtil.console.error(e.getMessage(), e);
        } finally {

            if (null != bos) try {
                bos.close();
            } catch (IOException e) {
                LogUtil.console.error(e.getMessage(), e);
            }

            if (null != zos) try {
                zos.close();
            } catch (IOException e) {
                LogUtil.console.error(e.getMessage(), e);
            }
        }
        return bytes;
    }

    public static byte[] unZip(byte[] data) {
        byte[] bytes = null;
        ByteArrayInputStream bis = null;
        ZipInputStream zis = null;
        ByteArrayOutputStream bos = null;

        try {
            bis = new ByteArrayInputStream(data);
            zis = new ZipInputStream(bis);
            bos = new ByteArrayOutputStream();

            while (null != zis.getNextEntry()) {
                byte[] bs = new byte[1024];
                int len;

                while (0 <= (len = zis.read(bs, 0, bs.length)))
                    bos.write(bs, 0, len);
                bytes = bos.toByteArray();
                bos.flush();
            }
        } catch (IOException e) {
            LogUtil.console.error(e.getMessage(), e);
        } finally {

            if (null != bos) try {
                bos.close();
            } catch (IOException e) {
                LogUtil.console.error(e.getMessage(), e);
            }

            if (null != zis) try {
                zis.close();
            } catch (IOException e) {
                LogUtil.console.error(e.getMessage(), e);
            }

            if (null != bis) try {
                bis.close();
            } catch (IOException e) {
                LogUtil.console.error(e.getMessage(), e);
            }
        }
        return bytes;
    }

    public static byte[] gZip(byte[] data) {
        byte[] bytes = null;
        ByteArrayOutputStream bos = null;
        GZIPOutputStream gos = null;

        try {
            bos = new ByteArrayOutputStream();
            gos = new GZIPOutputStream(bos);
            gos.write(data);
            bytes = bos.toByteArray();
        } catch (IOException e) {
            LogUtil.console.error(e.getMessage(), e);
        } finally {
            if (null != gos) try {
                gos.close();
            } catch (IOException e) {
                LogUtil.console.error(e.getMessage(), e);
            }

            if (null != bos) try {
                bos.close();
            } catch (IOException e) {
                LogUtil.console.error(e.getMessage(), e);
            }
        }
        return bytes;
    }

    public static byte[] unGZip(byte[] data) {
        byte[] bytes = null;
        ByteArrayInputStream bis = null;
        GZIPInputStream gis = null;
        ByteArrayOutputStream bos = null;

        try {
            bis = new ByteArrayInputStream(data);
            gis = new GZIPInputStream(bis);
            bos = new ByteArrayOutputStream();
            byte[] bs = new byte[1024];
            int len;

            while (-1 != (len = gis.read(bs, 0, bs.length)))
                bos.write(bs, 0, len);
            bytes = bos.toByteArray();
        } catch (IOException e) {
            LogUtil.console.error(e.getMessage(), e);
        } finally {
            if (null != bos) try {
                bos.close();
            } catch (IOException e) {
                LogUtil.console.error(e.getMessage(), e);
            }

            if (null != gis) try {
                gis.close();
            } catch (IOException e) {
                LogUtil.console.error(e.getMessage(), e);
            }

            if (null != bis) try {
                bis.close();
            } catch (IOException e) {
                LogUtil.console.error(e.getMessage(), e);
            }
        }
        return bytes;
    }
}
